package io.renren.modules.bs.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.bs.dto.ExtraDto;
import io.renren.modules.bs.entity.BsPatientEntity;
import io.renren.modules.bs.service.BsPatientService;
import io.renren.modules.bs.vo.ExtraPatientVo;
import io.renren.modules.bs.vo.LookDetailVo;
import io.renren.modules.bs.vo.PatientOptionVo;
import io.renren.modules.bs.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-15 23:03:15
 */
@RestController
@RequestMapping("bs/patient")
public class BsPatientController {
    @Autowired
    private BsPatientService bsPatientService;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bsPatientService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{patientId}")

    public R info(@PathVariable("patientId") Long patientId){
		BsPatientEntity bsPatient = bsPatientService.getById(patientId);

        return R.ok().put("bsPatient", bsPatient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody BsPatientEntity bsPatient){
		bsPatientService.save(bsPatient);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody BsPatientEntity bsPatient){
		bsPatientService.updateById(bsPatient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] patientIds){
		bsPatientService.removeByIds(Arrays.asList(patientIds));

        return R.ok();
    }

    /**
     * //根据患者名字
     * @param patientName
     * @return
     */
    @RequestMapping("/getOptionsByName")

    public R getOptionsByName(String patientName){

		List<PatientOptionVo> list = bsPatientService.getOptionsByName(patientName);
        return R.ok().put("data",list);
    }


    /**
     * 可抽取数
     * @param extraType
     * @param dataValue
     * @return
     */
    @RequestMapping("/getExtractableNumber")

    public R getExtractableNumber(Integer extraType,Long dataValue){

        Integer num = bsPatientService.getExtractableNumber(extraType,dataValue);
        return R.ok().put("data",num);
    }

    /**
     * 获得树数据
     * @param level
     * @return
     */
    @RequestMapping("/getTreeData")

    public R getTreeData(Integer level,String joinId){

        List<TreeVo> treeVos = this.bsPatientService.getTreeData(level,joinId);
        return R.ok().put("data",treeVos);
    }


    /**
     * 抽取
     * @param extraDtos
     * @return
     */
    @RequestMapping("/extra")
    public R extra(@RequestBody List<ExtraDto> extraDtos){

        try {
            List<ExtraPatientVo> extraPatientVos = this.bsPatientService.extra(extraDtos);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 抽取
     * @param extraNumber
     * @return
     */
    @RequestMapping("/randomExtra")
    public R randomExtra(Integer extraNumber){

        try {
            this.bsPatientService.randomExtra(extraNumber);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 抽取
     * @param extractDetailPatientId
     * @return
     */
    @RequestMapping("/getDetailData")
    public R getDetailData(Long extractDetailPatientId){

        LookDetailVo lookDetailVo = this.bsPatientService.getDetailData(extractDetailPatientId);
        return R.ok().put(lookDetailVo);
    }
}
